package Control;

import java.util.Comparator;

public class CommonWord {
	private String word;
	private int count;
	public static CommonWordComparator cwp = new CommonWord.CommonWordComparator();

	public CommonWord(String word) {
		super();
		this.word = word;
		this.count = 1;
	}



	public CommonWord(String word, int count) {
		super();
		this.word = word;
		this.count = count;
	}



	public String getWord() {
		return word.toLowerCase();
	}

	public int getCount() {
		return count;
	}

	public void increment(int value){
		this.count += value;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((word == null) ? 0 : word.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CommonWord other = (CommonWord) obj;
		if (word == null) {
			if (other.word != null)
				return false;
		} else if (!word.equalsIgnoreCase(other.word))
			return false;
		return true;
	}

public static final class CommonWordComparator implements Comparator{


		public int compare(Object o1, Object o2){
			if(o1.getClass() != CommonWord.class && o2.getClass() != CommonWord.class)
				return 0;
			CommonWord cw1 = (CommonWord)o1;
			CommonWord cw2 = (CommonWord)o2;
			if(cw1.getCount() > cw2.getCount())
				return -1;
			if(cw1.getCount() < cw2.getCount())
				return 1;
			return 0;
		}

		
	}



	@Override
	public String toString() {
		return "CommonWord =" + word + ", count=" + count;
	}



}
